//https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/?envType=study-plan-v2&envId=top-100-liked

#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <cmath>
#include <iostream>

using namespace std;

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        // write code here
        if(prices.empty()||prices.size() == 1) return 0;
        int min_num = *prices.begin();
        int res = 0;
        for(int j = 1;j<prices.size();++j) {
            if(min_num <=prices[j]) {
                res = std::max(res,prices[j]-min_num);   
            }
            min_num = prices[j] < min_num? prices[j]:min_num;
        }
        return res;

    }
};